package com.fengwk.support.math;

import java.io.Serializable;

/**
 * 坐标点
 * 
 * @author fengwk
 *
 */
public class P implements Serializable {

	private static final long serialVersionUID = -8464016772123826298L;

	public double x;
	public double y;

	public P(double x, double y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * 点到点距离
	 * 
	 * @return
	 */
	public double distance(P p) {
		return distance(p.x, p.y);
	}
	
	/**
	 * 点到点距离
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public double distance(double x, double y) {
		return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));
	}
	
	/**
	 * 中间点
	 * 
	 * @param p
	 * @return
	 */
	public P mid(P p) {
		return mid(x, y, p.x, p.y);
	}
	
	/**
	 * 中间点
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public P mid(double x, double y) {
		return mid(this.x, this.y, x, y);
	}
	
	private P mid(double x1, double y1, double x2, double y2) {
		return new P((x1 + x2) / 2d, (y1 + y2) / 2d);
	}
	
	/**
	 * 转为数组
	 * 
	 * @return
	 */
	public double[] toArray() {
		return new double[] {x, y};
	}
	
	@Override
	public String toString() {
		return "(" + x + ", " + y + ")";
	}
	
}
